package com.example.CarFee;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import com.example.trainfee.R;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

public class InfoActivity extends Activity {
	private EditText name, place, sex, phone;
	private Button saveBtn, cancleBtn;
	private String placeS = "";
	private String sexS = "";
	private String nameS = "";
	private String phoneS = "";
	private String[] info;
	private ImageView backBtn;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_info);
		name = (EditText) findViewById(R.id.name);
		sex = (EditText) findViewById(R.id.sex);
		place = (EditText) findViewById(R.id.place);
		phone = (EditText) findViewById(R.id.phone);
		saveBtn = (Button) findViewById(R.id.save);
		cancleBtn = (Button) findViewById(R.id.cancle);
		backBtn = (ImageView) findViewById(R.id.backBtn);
		backBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				finish();
			}
		});
		saveBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				try {
					placeS = URLEncoder.encode(place.getText().toString(),
							"UTF-8");
					sexS = URLEncoder.encode(sex.getText().toString(), "UTF-8");
					nameS = URLEncoder.encode(name.getText().toString(),
							"UTF-8");
					phoneS = URLEncoder.encode(phone.getText().toString(),
							"UTF-8");
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
				}
				Thread thread = new Thread(new Runnable() {

					@Override
					public void run() {
						if (Service
								.AddPersonalInfo(nameS, sexS, placeS, phoneS)) {
							handler.sendEmptyMessage(0x120);
						} else {
							handler.sendEmptyMessage(0x121);
						}
					}
				});
				thread.start();
			}
		});
		cancleBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				finish();
			}
		});
		getInfo();
	}

	private void getInfo() {
		Thread thread = new Thread(new Runnable() {

			@Override
			public void run() {
				info = Service.checkPersonalInfo();
				if (!info[0].equals("")) {
					handler.sendEmptyMessage(0x122);
				}
			}
		});
		thread.start();
	}

	private Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			if (msg.what == 0x120) {
				Toast.makeText(InfoActivity.this, "修改成功", Toast.LENGTH_SHORT)
						.show();
				finish();
			}
			if (msg.what == 0x121) {
				Toast.makeText(InfoActivity.this, "修改失败", Toast.LENGTH_SHORT)
						.show();
			}
			if (msg.what == 0x122) {
				Info.name = info[0];
				Info.sex = info[1];
				Info.place = info[2];
				Info.book = info[3];
				Info.station = info[4];
				Info.date = info[5];
				Info.time = info[6];
				if (info.length > 7) {
					Info.phone = info[7];
				}
				sex.setText(Info.sex);
				place.setText(Info.place);
				name.setText(Info.name);
				phone.setText(Info.phone);
			}
		}
	};
}
